package com.hngy.xpq.android_practice;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.hngy.xpq.android_practice.bean.Merchant;
import com.hngy.xpq.android_practice.database.SQLiteMerchant;
import com.hngy.xpq.android_practice.http.TcpClient;
import com.hngy.xpq.android_practice.merchant_main.MerchantMainActivity;
import com.hngy.xpq.android_practice.utils.Order;

public class MerchantActivity extends AppCompatActivity {

    private EditText phone,key;
    private Button dl,zc;
    private CheckBox bc;
    SQLiteMerchant sqLiteMerchant;
    SharedPreferences merchants;
    private ImageButton image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.merchant_dl);
        phone = findViewById(R.id.merchant_xpq_phone);
        key = findViewById(R.id.merchant_xpq_key);
        merchants = super.getSharedPreferences("merchants",MODE_PRIVATE);

        //为了节约开发时间，我就不重复用户的操作（保存密码）
        phone.setText(merchants.getString("name",""));
        key.setText(merchants.getString("key",""));

        //商家的数据库方法
        sqLiteMerchant = new SQLiteMerchant(this);

        //商家注册
        zc = findViewById(R.id.merchant_xpq_zc);
        zc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MerchantActivity.this,MerchantRegist.class);
                startActivity(intent);
            }
        });

        //商家登陆
        dl = findViewById(R.id.merchant_xpq_dl);
        dl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(phone.getText().toString())){
                    IndexToast("请输入账号");
                } else if (TextUtils.isEmpty(key.getText().toString())){
                    IndexToast("请输入密码");
                } else {
                    SharedPreferences.Editor editor = merchants.edit();
                    editor.putString("name",phone.getText().toString());
                    editor.putString("key" , key.getText().toString());
                    editor.commit();
                    String information = "DL002//" + phone.getText().toString() + "//" + key.getText().toString();
                    String a = new TcpClient().Transits(information);
                    if (a.equals("登录失败!")){
                        Toast.makeText(MerchantActivity.this,"账号或密码错误!!",Toast.LENGTH_SHORT).show();
                    } else {
                        Intent intent = new Intent(MerchantActivity.this, MerchantMainActivity.class);
                        startActivity(intent);
                    }
                    a = null;
                }
            }
        });

        //用户登陆
        image = findViewById(R.id.merchant_dl_image);
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MerchantActivity.this, OrderActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    public void IndexToast(String text){
        Toast.makeText(MerchantActivity.this,text,Toast.LENGTH_SHORT).show();
    }
}
